#include "configform.h"
#include "ui_configform.h"
#include <string.h>
#include <stdlib.h>

ConfigForm::ConfigForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ConfigForm)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":/image/logo.ico"));

    setupRealtimeDataDemo(ui->customPlot);
    ui->customPlot->replot();

}

void ConfigForm::setupRealtimeDataDemo(QCustomPlot *customPlot)
{
    customPlot->legend->setVisible(false); // 显示图例
    //customPlot->legend->setFont(QFont("Helvetica", 9)); // 设置图例字体
    customPlot->setAutoAddPlottableToLegend(false); // 添加graph后，自动显示图例。默认为true

    customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes | QCP::iSelectLegend | QCP::iSelectPlottables);
    QCPGraph * graph = customPlot->addGraph();
    graph->setPen(QPen(QColor(255, 0, 0)));
    graph->setLineStyle(QCPGraph::lsNone);
    graph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssCircle, 2));

    customPlot->xAxis->setLabel("X");
    customPlot->yAxis->setLabel("Y");

    customPlot->yAxis->setRange(-3000, 3000);
    customPlot->xAxis->setRange(-3000, 3000);
//    // make left and bottom axes transfer their ranges to right and top axes:
    connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
    connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));
}


ConfigForm::~ConfigForm()
{
    delete ui;
}


void ConfigForm::serialReadMsg(DataPack *data)
{

    if(data->package.type==MSG_MAG_FILTER)
    {

        float yaw ;
        memcpy(&yaw,&data->package.payload[0],4);
        ui->yaw->setText(QString::number(yaw));
        qDebug("%f",yaw);
    }
    else if(data->package.type==MSG_MAG_RAW){
        linA = (int16_t)(data->package.payload[0]<<8 | data->package.payload[1]);
        linB = (int16_t)(data->package.payload[2]<<8 | data->package.payload[3]);
        linC = (int16_t)(data->package.payload[4]<<8 | data->package.payload[5]);
        ui->customPlot->graph(0)->addData(linA, linB);
        ui->customPlot->replot(QCustomPlot::rpQueuedReplot);
    }
    else if(data->package.type==MSG_MAG_CALIBRATION){
        if(data->package.payload[0]==FINISH)  // 校准结束
        {
            linA=0;
            linB=0;
            linC=0;
            int ret = QMessageBox::warning(this, tr("information"),
                                             tr("Magnetometer calibration finish!"),
                                             QMessageBox::Ok,
                                             QMessageBox::Ok);
            ui->magStartBtn->setText("Magnetometer");
        }
    }
    else if(data->package.type==MSG_WIND_CALIBRATION){
        int ret = QMessageBox::warning(this, tr("information"),
                                         tr("Wind vane calibration finish!"),
                                         QMessageBox::Ok,
                                         QMessageBox::Ok);
    }

}




void ConfigForm::on_magStartBtn_clicked()
{
    if(ui->magStartBtn->text()=="Stop"){
        uint8_t data[6] = {0xFE,MSG_MAG_CALIBRATION ,0x01,FINISH,0x00,0x17};
        emit serialWriteData(data, 6);
        ui->magStartBtn->setText("Magnetometer");
    }else{
        uint8_t data[6] = {0xFE,MSG_MAG_CALIBRATION ,0x01,START,0x00,0x16};
        emit serialWriteData(data, 6);
        ui->magStartBtn->setText("Stop");
    }
}

void ConfigForm::on_windStartBtn_clicked()
{
    uint8_t data[6] = {0xFE,MSG_WIND_CALIBRATION ,0x01,START,0x00,0x1B};
    emit serialWriteData(data, 6);
}

void ConfigForm::on_clearnBtn_clicked()
{
    ui->customPlot->graph(0)->data().data()->clear();
    ui->customPlot->replot(QCustomPlot::rpQueuedReplot);
}
